/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-22     Administrator       the first version
 */
#include "GlobalVar.h"
#include <rtthread.h>
#include <rtdbg.h>
#include <unistd.h>
#include <stdio.h>

#include "ulog.h"


MECHINE_INFO machine_info;
USER_TASK task;

void GlobalVarInit(void)
{
    //machine_info.addr = GetMachineAddr();
    machine_info.address = 0x01;

}

void DelayMs(rt_uint32_t ms)
{
    rt_thread_mdelay(ms);
}


/*
要点提示:
1. float和unsigned long具有相同的数据结构长度
2. union据类型里的数据存放在相同的物理空间
*/
typedef union
{
    float fdata;
    unsigned long ldata;
}FloatLongType;

/*
将浮点数f转化为4个字节数据存放在byte[4]中
*/
void Float_to_Byte(float f,unsigned char byte[])
{
    FloatLongType fl;
    fl.fdata=f;
    byte[0]=(unsigned char)fl.ldata;
    byte[1]=(unsigned char)(fl.ldata>>8);
    byte[2]=(unsigned char)(fl.ldata>>16);
    byte[3]=(unsigned char)(fl.ldata>>24);
}
/*
将4个字节数据byte[4]转化为浮点数存放在*f中
*/
void Byte_to_Float(float *f,unsigned char byte[])
{
    FloatLongType fl;
    fl.ldata=0;
    fl.ldata=byte[3];
    fl.ldata=(fl.ldata<<8)|byte[2];
    fl.ldata=(fl.ldata<<8)|byte[1];
    fl.ldata=(fl.ldata<<8)|byte[0];
    *f=fl.fdata;
}


typedef union
{
    uint32_t uldata;
    unsigned long ldata;
}UintLongType;

/*
将浮点数f转化为4个字节数据存放在byte[4]中
*/
void Uint32t_to_Byte(uint32_t ul,uint8_t byte[])
{
    UintLongType ull;
    ull.uldata=ul;
    byte[0]=(unsigned int)ull.ldata;
    byte[1]=(unsigned int)(ull.ldata>>8);
    byte[2]=(unsigned int)(ull.ldata>>16);
    byte[3]=(unsigned int)(ull.ldata>>24);
}
/*
将4个字节数据byte[4]转化为浮点数存放在*f中
*/
void Byte_to_Uint32t(uint32_t *ul,uint8_t byte[])
{
    UintLongType ull;
    ull.uldata=0;
    ull.ldata=byte[3];
    ull.ldata=(ull.ldata<<8)|byte[2];
    ull.ldata=(ull.ldata<<8)|byte[1];
    ull.ldata=(ull.ldata<<8)|byte[0];
    *ul=ull.uldata;
}

/*
CRC8
*/
unsigned int cal_crc_table(unsigned int *ptr, unsigned int len)
{
    unsigned int  crc = 0x00;

    while (len--)
    {
        crc = crc_table[crc ^ *ptr++];
    }
    return (crc);
}
